package com.area.controller;


import com.area.entity.Area;
import com.area.entity.vo.AreaVo;
import com.area.service.IAreaService;
import com.code.domain.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * BASIC_地区 前端控制器
 * </p>
 *
 * @author guanfenga@yonyou.com
 * @since 2022-08-17
 */
@ApiOperation(value = "区域", notes = "区域")
@Controller // 相当于控制层类上加@Controller + 方法上加 @ResponseBody
@RequestMapping("/area")
@Slf4j
@CrossOrigin
public class AreaController {

    @Autowired
    private IAreaService areaService;

    @ApiOperation(value = "查询区域分页", notes = "查询区域分页")
    @GetMapping(value = "/queryAreaPage")
    public @ResponseBody
    AjaxResult queryAreaPage(@RequestBody(required = false) PageParam<Area> pageParam, Area area, HttpServletRequest request, HttpServletResponse response) {
        return AjaxResult.success("操作成功", areaService.queryAreaPage(pageParam, area));
    }

    @ApiOperation(value = "新增区域", notes = "新增区域")
    @PostMapping(value = "/save")
    public @ResponseBody AjaxResult saveArea(@RequestBody Area area, HttpServletRequest request, HttpServletResponse response) {
        int result = areaService.saveArea(area);
        return AjaxResult.success(result);
    }

    @ApiOperation(value = "修改区域", notes = "修改区域")
    @PutMapping("/update")
    public @ResponseBody AjaxResult updateArea(@RequestBody Area area, HttpServletRequest request, HttpServletResponse response) {
        int result = areaService.updateArea(area);
        return AjaxResult.success(result);
    }

    /*
     * 区划首页
     * @param model
     * @return
     */
    @RequestMapping("/toAreaIndex")
    @ApiOperation(value = "跳转区域", notes = "跳转区域")
    public String toAreaIndex(Model model, HttpServletRequest request, HttpServletResponse response) {
        return "area/area_index";
    }

    @GetMapping(value = "/findAreaPage")
    public @ResponseBody
    JSONObject findAreaPage(Integer page, Integer limit, Area area, HttpServletRequest request, HttpServletResponse response) {
        List<Area> countList = areaService.queryAreaList(area);
        List<Map<String, Object>> areaList = areaService.findAreaPage(GpmartUtil.PageBefore(page, limit), limit);
        return GpmartUtil.layJson(areaList, countList.size());
    }


    @GetMapping(value = "/queryAreaLayUiPage")
    @ApiOperation(value = "前端框架分页查询", notes = "前端框架分页查询")
    public @ResponseBody
    CommonResult queryAreaLayUiPage(AreaVo area, HttpServletRequest request, HttpServletResponse response) {
        List<Area> countList = areaService.queryAreaList(area);
        List<Area> list = areaService.findAreaPage(GpmartUtil.PageBefore(area.getPage(), area.getLimit()), area.getLimit(), area);
        return new CommonResult(0, "操作成功", countList.size(), list);
    }


    @GetMapping(value = "/queryAreaListAll")
    public CommonResult queryAreaListAll() {
        boolean result = areaService.getAreaToRedis();
        return new CommonResult();
    }

    @ApiOperation(value = "前端框架通过Redis进行分页", notes = "前端框架通过Redis进行分页")
    @GetMapping(value = "/queryAreaLayUiRedisPage")
    public @ResponseBody CommonResult queryAreaLayUiRedisPage(AreaVo areaVo, HttpServletRequest request, HttpServletResponse response) {
        List<Area> areaList = areaService.queryAreaLayUiRedisPage(areaVo);
        return new CommonResult(0, "操作成功", areaList.size(), areaList);
    }

    @ApiOperation(value = "查询所有地区", notes = "查询所有地区")
    @GetMapping(value = "/queryAreaAll")
    public @ResponseBody AjaxResult queryAreaAll(AreaVo areaVo, HttpServletRequest request, HttpServletResponse response) {
        List<Area> list = areaService.queryAreaList(areaVo);
        System.out.println("list = " + list);
        return new AjaxResult(0, "操作成功", list);
    }


}
